package MyGO.controller;

import MyGO.Form.TrackForm;
import MyGO.dao.ITrackFormDao;
import MyGO.dao.impl.TrackFormImpl;
import MyGO.property.Medicine;
import MyGO.service.IMedicineService;
import MyGO.service.impl.MedicineServiceImpl;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Fayao {
    @FXML
    private TextField binglihao;
    @FXML
    private TableView<TrackForm> dingdan;
    @FXML
    private Button sure;
    @FXML
    private Button fayao;
    @FXML
    private TextField xingming;
    @FXML
    private TableView<Medicine> yao;
    private ITrackFormDao trackFormService = new TrackFormImpl();
    private IMedicineService medicineService = new MedicineServiceImpl();
    private List<TrackForm> list1; //人
    private List<Medicine> list2 = new ArrayList<>(); //药
    private TrackForm user=null;

    public void initialize() {
        list1 = trackFormService.selectTrackFormByMedicine("未发药");
        showPeople();
        //搜索
        sure.setOnAction(event -> {
            list1.clear();
            if (xingming.getText()!=null || binglihao.getText()!=null){
                if (xingming.getText()!=null)
                    list1 = trackFormService.selectTrackFormByName(xingming.getText());
                else
                    list1 = trackFormService.selectTrackFormById(binglihao.getText());
                if (list1.isEmpty()){
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setHeaderText("该病人不存在！");
                    xingming.clear();
                    binglihao.clear();
                    alert.showAndWait();
                }else{
                    //判断条件
                    if (Objects.equals(list1.get(0).getStatus_vail(), "已发药")){
                        list1.clear();
                        Alert alert = new Alert(Alert.AlertType.ERROR);
                        alert.setHeaderText("该病人已发药！");
                        alert.showAndWait();
                    }else if (Objects.equals(list1.get(0).getStatus_vail(), "未缴费")){
                        list1.clear();
                        Alert alert = new Alert(Alert.AlertType.ERROR);
                        alert.setHeaderText("该病人未缴费！");
                        alert.showAndWait();
                    }else{
                        dingdan.getColumns().clear();
                        showPeople();
                    }
                }
            }else{
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setHeaderText("搜索条件不能为空！");
            }
        });
        //发药
        fayao.setOnAction(event -> {
            if (user==null){
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setHeaderText("请选择病人！");
                alert.showAndWait();
            }else{
                user.setStatus_vail("已发药");
                //减少药的数量
                for (String ID : user.getMedicineForm().getMedicines()) {
                    medicineService.getMedicineMap().get(ID).deleteNumber(user.getMedicineForm().getMedicineNum().get(ID));
                }

                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("发药成功！请重新选择病人");
                xingming.clear();
                binglihao.clear();
                dingdan.getColumns().clear();
                showPeople();
                yao.getColumns().clear();
                alert.showAndWait();
                user = null;
                list2.clear();
            }
        });
    }

    //打印人
    private void showPeople() {
        TableColumn<TrackForm, Number> indexColumn = new TableColumn<>("编号");
        indexColumn.setSortable(false);
        indexColumn.setCellFactory(column -> new TableCell<TrackForm, Number>() {
            @Override
            protected void updateItem(Number item, boolean empty) {
                super.updateItem(item, empty);

                if (empty || getIndex() < 0) {
                    setText(null);
                } else {
                    setText(Integer.toString(getIndex() + 1));
                }
            }
        });

        // 确保编号列是第一列
        dingdan.getColumns().add(0, indexColumn);
        //添加其他列
        TableColumn<TrackForm, String> nameColumn = new TableColumn<>("姓名");
        TableColumn<TrackForm, String> idColumn = new TableColumn<>("病历号");
        TableColumn<TrackForm, String> ageColumn = new TableColumn<>("年龄");
        TableColumn<TrackForm, String> sexColumn = new TableColumn<>("性别");
        nameColumn.setPrefWidth(100);
        idColumn.setPrefWidth(400);
        ageColumn.setPrefWidth(100);
        sexColumn.setPrefWidth(200);

        nameColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPatientInfoForm().getName()));
        idColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPatientInfoForm().getId()));
        ageColumn.setCellValueFactory(cellData -> new SimpleStringProperty(String.valueOf(cellData.getValue().getPatientInfoForm().getYear())));
        sexColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPatientInfoForm().getGender()));
        ObservableList<TrackForm> data = FXCollections.observableArrayList(list1);
        dingdan.setItems(data);
        dingdan.getColumns().addAll(nameColumn,idColumn,ageColumn,sexColumn);

        // 设置按钮
        TableColumn<TrackForm, Void> actionCol = new TableColumn<>("操作");

        actionCol.setCellFactory(param -> {
            Button deleteButton = new Button("显示药品");
            TableCell<TrackForm, Void> cell = new TableCell<TrackForm, Void>() {
                @Override
                protected void updateItem(Void item, boolean empty) {
                    super.updateItem(item, empty);

                    if (empty) {
                        setGraphic(null);
                    } else {
                        setGraphic(deleteButton);
                    }
                }
            };

            deleteButton.setOnAction(event -> {
                // 获取当前行的数据
                user = cell.getTableRow().getItem();
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setContentText("选择完成，请进行下一步操作");
                alert.showAndWait();

                showMedicine();
            });
            return cell;
        });
        dingdan.getColumns().add(actionCol);
    }

    //打印药
    private void showMedicine() {
        if (user==null){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("阿sir你还没选病人呢");
            alert.showAndWait();
        }else{
            //初始化
            list2.clear();
            yao.getColumns().clear();
            for (String ID : user.getMedicineForm().getMedicines()) {
                list2.add(medicineService.getMedicineMap().get(ID));
            }

            //设置显示界面
            TableColumn<Medicine, Number> indexColumn = new TableColumn<>("编号");
            indexColumn.setSortable(false);
            indexColumn.setCellFactory(column -> new TableCell<Medicine, Number>() {
                @Override
                protected void updateItem(Number item, boolean empty) {
                    super.updateItem(item, empty);

                    if (empty || getIndex() < 0) {
                        setText(null);
                    } else {
                        setText(Integer.toString(getIndex() + 1));
                    }
                }
            });

            // 确保编号列是第一列
            yao.getColumns().add(0, indexColumn);
            //添加其他列
            TableColumn<Medicine, String> nameColumn = new TableColumn<>("名称");
            TableColumn<Medicine, String> idColumn = new TableColumn<>("编码");
            TableColumn<Medicine, String> sizeColumn = new TableColumn<>("规格");
            TableColumn<Medicine, String> buildColumn = new TableColumn<>("厂家");
            TableColumn<Medicine, String> numberColumn = new TableColumn<>("数量");
            nameColumn.setPrefWidth(100);
            idColumn.setPrefWidth(400);
            sizeColumn.setPrefWidth(100);
            buildColumn.setPrefWidth(200);
            numberColumn.setPrefWidth(100);

            nameColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
            idColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId()));
            sizeColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getFormat()));
            buildColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getManufacturer()));
            numberColumn.setCellValueFactory(cellData -> new SimpleStringProperty(String.valueOf(user.getMedicineForm().getMedicineNum().get(cellData.getValue().getId()))));
            ObservableList<Medicine> data = FXCollections.observableArrayList(list2);
            yao.setItems(data);
            yao.getColumns().addAll(idColumn,nameColumn,sizeColumn,buildColumn,numberColumn);
        }
    }
}
